<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class ModelScore extends base {

	/**
	 * 积分原子操作：会员积分改变
	 * 修改指定的会员的用户总积分
	 * 
	 * @param int $uid
	 * @param int $score
	 * @return int
	 */
	function member_score( $uid, $score ) {
		$sql = "UPDATE $this->tmember SET memberscore=memberscore+'{$score}' WHERE id='{$uid}' LIMIT 1";
		model::dbuser()->exe ( $sql );
		return model::dbuser()->update_rows();
	}

	/**
	 * 积分原子操作：相片的积分改变
	 * 修改某相片的总积分
	 *
	 * @param int $info_id
	 * @param int $score
	 * @param float $time_limit
	 * @return boolean
	 */
	function pix_score( $info_id, $score ) {
		$sql = "UPDATE $this->tpix SET info_score=info_score+'{$score}' WHERE id='{$info_id}' LIMIT 1";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}

	/**
	 * 留言或者相片的不算点击的时间是否已经超过
	 *
	 * @param string $uid
	 * @param int $info_id
	 * @param string $info_type
	 * @param float $time_limit
	 * @return int
	 */
	function time_out( $uid, $info_id, $time_limit = 0, $info_type = 'pix' ) {
		$type = ( $info_type == 'msg' ) ? 0 : 1;
		$time = '';
		$timeout = 1;
		if ( $time_limit > 0 ) {
			$right_time = date( 'Y-m-d H:i:s', time() - $time_limit * 60 * 60 );
			$sql = "SELECT * FROM $this->tscore WHERE uid='$uid' AND target_id='$info_id' AND target_type='$type' LIMIT 1";
			$aTO = model::dbuser()->get_one( $sql );
			$timeout = intval( empty( $aTO ) || ($aTO['lasttime'] < $right_time) );
		}
		return $timeout;
	}

	/**
	 * 检查用户是否点击、支持、反对过某条留言或相片
	 *
	 * @param int $info_id
	 * @param strint $info_type
	 * @return int
	 */
	function get_viewtime ( $uid, $info_id, $info_type = 'pix' ) {
		$info_type = ( $info_type == 'msg' ? 0 : 1 );
		$sql = "SELECT * FROM $this->tscore WHERE uid='{$uid}' AND target_id={$info_id} AND target_type={$info_type} LIMIT 1";
		return model::dbuser()->get_one( $sql );
	}

	/**
	 * 更新留言或者相片的最后点击时间
	 *
	 * @param string $uid
	 * @param int $info_id
	 * @param string $info_type
	 * @return int
	 */
	function update_viewtime( $uid, $info_id, $info_type = 'pix' ) {
		$info_type = ( $info_type == 'msg' ? 0 : 1 );
		$sql = "INSERT INTO $this->tscore SET uid='{$uid}', target_id='{$info_id}', target_type='{$info_type}', lasttime=NOW()
				ON DUPLICATE KEY UPDATE lasttime=NOW()";
		model::dbuser()->exe( $sql );
		return model::dbuser()->update_rows();
	}

}